package com.f.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import com.f.javabean.PushMsg.Push;
import com.google.protobuf.ByteString;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class FHandle extends ChannelInboundHandlerAdapter{

	private String allFilePath = "E:/filehandle/filein/";
	//private ConcurrentMap<String,SendFileThread> sendFileThreadMap = new ConcurrentHashMap<String, FHandle.SendFileThread>();
	
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		super.channelActive(ctx);
	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		super.channelInactive(ctx);
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		if(msg instanceof Push){
			Push push = (Push)msg;
			System.out.println(push.getId().length());
			String fileName = push.getFilename();
			String filePath = allFilePath + fileName;
			File file = new File(filePath);
			if(file.exists()){//start
				//new Thread(new SendFileThread(ctx.channel(), file, "start","")).start();
				new Thread(new SendFileThread(ctx.channel(), push.getId(), push.getType(), filePath)).start();
			}
		}else{
			super.channelRead(ctx, msg);
		}
	}
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		// TODO Auto-generated method stub
		super.exceptionCaught(ctx, cause);
	}

	public static void main(String[] args) {
		DecimalFormat df = new DecimalFormat("######0.0");     
		double d = 1024D/10392D;
		System.out.println(d);
		d = Double.parseDouble(df.format(d));
		System.out.println(d);
	}
	
}
